<?php

/**
 * 栏目管理 Service
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ContypeService.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Service;
class ContypeService extends CommonService{
    private $idArray;
    private $idCounter;
    
    public function __construct() {
        $this->idArray = '';
        $this->idCounter = 0;
    }
    /**
     * 得到栏目的详细信息
     * @param  array $where   查询条件
     * @param  string $fields 插叙字段
     * @return array
     */
    public function getPagination($where, $fields,$order,$firstRow,$listRows, $relation) {
        return parent::getPagination($where, 
                                        $fields,
                                        $order,
                                        $firstRow,
                                        $listRows,
                                        $relation);
    }

    /**
     * 得到所有的栏目
     * @param  string $fields 查询字段
     * @param  string  $order 排序
     * @return array
     */
    public function getAll($fields, $order, $relation = false, $where = '') {
        return $this->getPagination($where, $fields, $order, null, null, $relation);
    }
    
    /**
     * 添加栏目数据
     * @param type $data
     */
    public function add($data) {
        $TypeModel = $this->getD();
        if(false === ($types = $TypeModel->create($data))){
            return $this->errorResultReturn($TypeModel->getError());
        }
        if(false === $TypeModel->add($types)){
            return $this->errorResultReturn('系统出错了！');
        }
        return $this->resultReturn(true);
    }
    
    /**
     * 更新数据
     * @param type $data
     * @return type
     */
    public function update($data) {
        $TypeModel = $this->getD();
        if(false === ($types = $TypeModel->create($data))){
            return $this->errorResultReturn($TypeModel->getError());
        }
        if(false === $TypeModel->save($types)){
            return $this->errorResultReturn('系统出错了！');
        }
        return $this->resultReturn(true);
    }
    
    /**
     * 更新排序
     * @param type $data
     * @return type
     */
    public function upRankAll($data) {
        if(!isAssocArray($data)){
            return $this->errorResultReturn('无效操作！');
        }
        $result = $this->getAll("id", "id asc", false);
        foreach($result as $val){
            if(isset($data['sortrank'.$val['id']])){
                $this->getM()->where(array('id' => $val['id']))->setField('sortrank', intval($data['sortrank'.$val['id']]));
            }
        }
        return $this->resultReturn(true);
    }
    
    public function delete($id) {
        $typeinfos = $this->getById($id);
        if(!isArrayAndNotEmpty($typeinfos)){
            return $this->errorResultReturn("无效操作！");
        }
        $this->getSonId($id);
        foreach ($this->idArray as $id) {
            $this->getD()->delete($id);//删除栏目

            //删除数据
            M("Content")->where(array("typeid" => $id))->delete();//删除文档
            if($typeinfos['addtable']){
                M($typeinfos['addtable'])->where(array("typeid" => $id))->delete();//删除附加表
            }
        }
        @reset($this->idArray);
        $this->idCounter = 0;
        return $this->resultReturn(true);
    }
    /**
     * 得到带又层级的node数据
     * @return array
     */
    public function getTypes(array $where) {
        $category = new \Org\Util\Category($this->getModelName(),
                                           array('id', 'reid', 'typename'));
        /*$where = array();
        if($model_id != 0){
            $where['model_id'] = $model_id;
        }
        $where['ispart'] = array("neq", 2);*/
        $types = $category->getList($where, 0, 'sortrank asc');
        return D("Contype", "Logic")->FormatType($types);
    }
    
    /**
     * 按id得到栏目数据
     * @param  int     $id
     * @return array
     */
    public function getById($id, $relation = true) {
        $type = $this->getD()->relation($relation)->getById($id);
        if (empty($type)) {
            return null;
        }
        return $type;
    }
    
    /**
     * 获取子ID
     * @param type $reid
     * @param type $model_id
     * @return type
     */
    public function getSonId($reid, $model_id = 0) {
        $this->idArray = '';
        return $this->getSonIds($reid, $model_id);
    }
    /**
     * 获取子ID
     * @param type $reid
     * @param type $model_id
     * @return type
     */
    private function getSonIds($reid, $model_id = 0) {
        $this->idArray[$this->idCounter] = $reid;
        $this->idCounter++;
        $where['reid'] = $reid;
        if($model_id != 0){
            $where['model_id'] = $model_id;
        }
        $type = $this->getD()->where($where)->field("id")->select();
        if(isArrayAndNotEmpty($type)){
            foreach ($type as $value) {
                $this->getSonIds($value['id'], $model_id);
            }
        }
        return $this->idArray;
    }
    
    /**
     *  检测模型ID
     *
     * @access    public
     * @param     int  $typeid  栏目ID
     * @param     int  $mid  模型ID
     * @return    bool
     */    
    public function CheckModel($typeid, $mid) {
        $typeinfo = $this->getById($typeid);
        if($typeinfo['ispart'] != 0 || $typeinfo['model_id'] != $mid){
            return false;
        }else{
            return true;
        }
    }
    protected function getModelName() {
        return 'Contype';
    }
}
